﻿using System;
using System.Windows;
using System.Windows.Controls;

using Perseus;
using Perseus.Data;
using Perseus.Plugins;

using Hetymine.Executioner.Data;
using Hetymine.Executioner.Plugins;

namespace Hetymine.Executioner.Plugins.General.Notes {
    /// <summary>
    /// Interaction logic for NotesConfigurator.xaml
    /// </summary>
    public partial class NotesConfigurator : UserControl, IConfigurator<IniFile> {
        private IniFile _Config;
        private bool _IsApply = true;  

        public NotesConfigurator() {
            InitializeComponent();

            // Call String
            this.cCallString.TextChanged += new TextChangedEventHandler(CallString_TextChanged);

            // Window Positioning and Behaviour (Static)
            this.cAlignment.SelectionChanged += new SelectionChangedEventHandler(Alignment_SelectionChanged);
            this.cLeft.TextChanged += new TextChangedEventHandler(Left_TextChanged);
            this.cTop.TextChanged += new TextChangedEventHandler(Top_TextChanged);
            this.cDisplay.SelectionChanged += new SelectionChangedEventHandler(Display_SelectionChanged);            
            this.cShowInTaskbar.Click += new RoutedEventHandler(ShowInTaskbar_Click);
            this.cTopmost.Click += new RoutedEventHandler(Topmost_Click);

            // Window Positioning and Behaviour (Timeout)
            this.cAlignmentTimeout.SelectionChanged += new SelectionChangedEventHandler(AlignmentTimeout_SelectionChanged);
            this.cLeftTimeout.TextChanged += new TextChangedEventHandler(LeftTimeout_TextChanged);
            this.cTopTimeout.TextChanged += new TextChangedEventHandler(TopTimeout_TextChanged);
            this.cDisplayTimeout.SelectionChanged += new SelectionChangedEventHandler(DisplayTimeout_SelectionChanged);            
            this.cShowInTaskbarTimeout.Click += new RoutedEventHandler(ShowInTaskbarTimeout_Click);
            this.cTopmostTimeout.Click += new RoutedEventHandler(TopmostTimeout_Click);
        }

        private void CallString_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetString("Notes", "Call String", this.cCallString.Text);
        }

        private void Alignment_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            ComboBoxItem cb = e.AddedItems[0] as ComboBoxItem;
            string alignment = cb.Content.ToString().ToLower();
            this._Config.SetString("Notes.Static", "Alignment", alignment);
        }
        private void Left_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            int value;
            if (!int.TryParse(this.cLeft.Text, out value)) {
                value = 0;
            }

            this._Config.SetInt("Notes.Static", "Left", value);
        }
        private void Top_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            int value;
            if (!int.TryParse(this.cTop.Text, out value)) {
                value = 0;
            }

            this._Config.SetInt("Notes.Static", "Top", value);
        }
        private void Display_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetInt("Notes.Static", "Display", this.cDisplay.DisplayIndex);
        }        
        private void ShowInTaskbar_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetBool("Notes.Static", "Show In Taskbar", cShowInTaskbar.IsChecked.Value);
        }
        private void Topmost_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetBool("Notes.Static", "Topmost", cTopmost.IsChecked.Value);
        }

        private void AlignmentTimeout_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            ComboBoxItem cb = e.AddedItems[0] as ComboBoxItem;
            string alignment = cb.Content.ToString().ToLower();
            this._Config.SetString("Notes.Timeout", "Alignment", alignment);
        }
        private void LeftTimeout_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            int value;
            if (!int.TryParse(this.cLeftTimeout.Text, out value)) {
                value = 0;
            }

            this._Config.SetInt("Notes.Timeout", "Left", value);
        }
        private void TopTimeout_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            int value;
            if (!int.TryParse(this.cTopTimeout.Text, out value)) {
                value = 0;
            }

            this._Config.SetInt("Notes.Timeout", "Top", value);
        }
        private void DisplayTimeout_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetInt("Notes.Timeout", "Display", this.cDisplayTimeout.DisplayIndex);
        }        
        private void ShowInTaskbarTimeout_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetBool("Notes.Timeout", "Show In Taskbar", cShowInTaskbarTimeout.IsChecked.Value);
        }
        private void TopmostTimeout_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetBool("Notes.Timeout", "Topmost", cTopmostTimeout.IsChecked.Value);
        }

        #region IConfigurator<IniFile>
        public void InitializeConfig(IniFile config) {
            this._Config = config;

            this._IsApply = false;

            // Command
            this.cCallString.Text = this._Config.GetString("Notes", "Call String", "note");

            // Window Positioning and Behaviour (Static)
            this.cAlignment.Alignment = this._Config.GetString("Notes.Static", "Alignment", "centre centre");
            this.cLeft.Text = this._Config.GetInt("Notes.Static", "Left", 0).ToString();
            this.cTop.Text = this._Config.GetInt("Notes.Static", "Top", 0).ToString();
            this.cDisplay.DisplayIndex = this._Config.GetInt("Notes.Static", "Display", -1);            
            this.cShowInTaskbar.IsChecked = this._Config.GetBool("Notes.Static", "Show In Taskbar", false);
            this.cTopmost.IsChecked = this._Config.GetBool("Notes.Static", "Topmost", false);


            // Window Positioning and Behaviour (Timeout)
            this.cAlignmentTimeout.Alignment = this._Config.GetString("Notes.Timeout", "Alignment", "centre centre");
            this.cLeftTimeout.Text = this._Config.GetInt("Notes.Timeout", "Left", 0).ToString();
            this.cTopTimeout.Text = this._Config.GetInt("Notes.Timeout", "Top", 0).ToString();
            this.cDisplayTimeout.DisplayIndex = this._Config.GetInt("Notes.Timeout", "Display", -1);            
            this.cShowInTaskbarTimeout.IsChecked = this._Config.GetBool("Notes.Timeout", "Show In Taskbar", false);
            this.cTopmostTimeout.IsChecked = this._Config.GetBool("Notes.Timeout", "Topmost", false);

            if (System.Windows.Forms.Screen.AllScreens.Length <= 1) {
                this.cDisplayLabel.Visibility = Visibility.Collapsed;
                this.cDisplay.Visibility = Visibility.Collapsed;
                this.cDisplayLabelTimeout.Visibility = Visibility.Collapsed;
                this.cDisplayTimeout.Visibility = Visibility.Collapsed;
            }

            this._IsApply = true;
        }
        public UserControl ConfigPanel { get { return this; } }
        #endregion
    }
}
